<?php 
class My_Vk
{    
    CONST APP_ID = '3699785';
    CONST REDIRECT_URI = 'http://elfac.p.ht/login/vk-auth';
    CONST RESPONSE_TYPE = 'code';
    CONST APP_SECRET = 'dfadsf34rf3f3';
    CONST SCOPE = 'offline';
    CONST AUTH_URI = 'http://api.vkontakte.ru/oauth/authorize';
    CONST ACCESS_TOKEN_URI = 'https://api.vkontakte.ru/oauth/access_token';
    CONST API_URL = 'https://api.vk.com/method/';    

    protected $_accessToken = '';

    public function __construct($accessToken=null) {        
        $this->_accessToken = $accessToken;        
    }

    public function getAuthUrl() {
        $params = array(
            'client_id'     => self::APP_ID,
            'scope'         => self::SCOPE,
            'redirect_uri'  => self::REDIRECT_URI,
            'response_type' => self::RESPONSE_TYPE
        );
        $urlParams = urldecode(http_build_query($params));
        return self::AUTH_URI . '?' .  $urlParams;        
    }

    public function getAccessToken($code) {
        $params = array(
            'client_id'     => self::APP_ID,
            'client_secret' => self::APP_SECRET,
            'code'          => $code,
            'redirect_uri'  => self::REDIRECT_URI
        );
        $urlParams = urldecode(http_build_query($params));
        $url = self::ACCESS_TOKEN_URI . '?' . $urlParams;        
        $response = Zend_Json::decode(file_get_contents($url));
        $this->_accessToken = $response['access_token'];
        return $response;
    }

    public function request($methodName, $params=array()) {
        $url = self::API_URL . $methodName . '?';
        if ($params) {
            $url .= urldecode(http_build_query($params)) . '&';
        }
        $url .= 'access_token=' . $this->_accessToken;         
        return Zend_Json::decode(file_get_contents($url));
    }
}